package com.zhika.zishi.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.zhika.zishi.R;
import com.zhika.zishi.adapter.FriendsDynamicAdapter;
import com.zhika.zishi.beans.FriendsDynamicBean;
import com.zhika.zishi.constant.OthersConstant;
import com.zhika.zishi.customview.ChooseHomepageView;
import com.zhika.zishi.customview.LoadListView;
import com.zhika.zishi.customview.RefreshableView;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.interfaces.ChooseHomepageInterface;
import com.zhika.zishi.interfaces.TitleInterface;
import com.zhika.zishi.util.LogUtil;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/13.
 */
public class FriendsDynamicActivity extends BaseActivity implements TitleInterface {

    private static final String TAG = "FriendsDynamicActivity";

    private LoadListView listView;

    private RefreshableView refreshableView;

    private ArrayList<FriendsDynamicBean> friendsDynamicList = new ArrayList<>();

    private FriendsDynamicAdapter dynamicAdapter;

    private TitleView titleView;

    private ChooseHomepageView chooseHomepageView;

    //网络请求对象
    private AsyncHttpClient asyncHttpClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_dynamic_layout);

        addTestData();

        initView();
    }

    private void initView() {

        asyncHttpClient = new AsyncHttpClient();

        refreshableView = (RefreshableView) findViewById(R.id.friends_dynamic_refresh_header_view);
        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                LogUtil.e(TAG, "更新数据");
                refreshListener();
            }
        }, OthersConstant.FRIENDS_DYNAMIC_REFRESH);//1是一个id,是为了防止在不同的界面刷新时发生冲突

        listView = (LoadListView) findViewById(R.id.friends_dynamic_list_view);
        dynamicAdapter = new FriendsDynamicAdapter(FriendsDynamicActivity.this, friendsDynamicList);
        listView.setAdapter(dynamicAdapter);
        listView.setInterface(new LoadListView.ILoadListener() {
            @Override
            public void onLoad() {
                LogUtil.e(TAG, "加载更多");
                new OnLoadTask().execute();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(FriendsDynamicActivity.this, "listView的点击事件", Toast.LENGTH_SHORT).show();
                chooseHomepageView.setVisibility(View.GONE);
            }
        });


        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                chooseHomepageView.setVisibility(View.GONE);
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

        titleView = (TitleView) findViewById(R.id.friends_dynamic_title_view);
        titleView.setTitleViewInterface(this);
        titleView.setMiddleRowText("好友动态");

        chooseHomepageView = (ChooseHomepageView) findViewById(R.id.friends_dynamic_choose_homepage_view);
        chooseHomepageView.setFirstText("推荐");
        chooseHomepageView.setSecondText("频道");
        chooseHomepageView.setChooseHomepageInterface(new ChooseHomepageInterface() {
            @Override
            public void firstHomepageTextListener() {
                Intent intent = new Intent(FriendsDynamicActivity.this, RecommendHomepageActivity.class);
                startActivity(intent);
                chooseHomepageView.setVisibility(View.GONE);
            }

            @Override
            public void secondHomepageTextListener() {
                Intent intent = new Intent(FriendsDynamicActivity.this, ChannelActivity.class);
                startActivity(intent);
                chooseHomepageView.setVisibility(View.GONE);
            }
        });
    }

    private void addTestData() {
        for (int i = 0; i < 10; i++) {
            FriendsDynamicBean bean = new FriendsDynamicBean();
            bean.setNickname("昵称");
            bean.setStatusStr("发表状态");
            bean.setTimeStr("发表时间");
            bean.setStrategyName("攻略标题");
            bean.setCollectNumStr("收藏");
            bean.setCommentNumStr("评论");
            bean.setLikeNumStr("关注");

            friendsDynamicList.add(bean);
        }
    }


    /**
     * 下拉刷新的任务
     */
    private void refreshListener() {
        String url = "";

        /**
         * 网络请求的真正事件
         */
//        asyncHttpClient.get(FriendsDynamicActivity.this,url,new AsyncHttpResponseHandler() {
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//
//                //从网络上请求到的数据
//                String jsonData = new String(responseBody);
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//
//            }
//        });

        FriendsDynamicBean bean = new FriendsDynamicBean();
        bean.setNickname("跟新昵称");
        bean.setStatusStr("更新状态");
        bean.setTimeStr("更新时间");
        bean.setStrategyName("更新攻略标题");
        bean.setCollectNumStr("g收藏");
        bean.setCommentNumStr("g评论");
        bean.setLikeNumStr("g关注");

        //0代表把数据添加到ListView的最顶端
        friendsDynamicList.add(0, bean);

        //刷新完成
        refreshableView.finishRefreshing();

        //最后通知适配器更新ListView
        dynamicAdapter.notifyDataSetChanged();

    }

    /**
     * 加载更多事件的真正任务
     */
    private class OnLoadTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            FriendsDynamicBean bean = new FriendsDynamicBean();
            bean.setNickname("加载更多昵称");
            bean.setStatusStr("加载更多状态");
            bean.setTimeStr("时间");
            bean.setStrategyName("加载更多攻略标题");
            bean.setCollectNumStr("g收藏");
            bean.setCommentNumStr("g评论");
            bean.setLikeNumStr("g关注");

            //0代表把数据添加到ListView的最顶端
            friendsDynamicList.add(bean);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            //加载完成
            listView.loadComplete();

            //通知ListView更新界面
            dynamicAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void leftTitleListener() {
        Intent intent = new Intent(FriendsDynamicActivity.this,MySelfInfoActivity.class);
        startActivity(intent);
    }

    @Override
    public void middleTitleListener() {
        chooseHomepageView.setVisibility(View.VISIBLE);
    }

    @Override
    public void rightTitleListener() {
        Intent intent = new Intent(FriendsDynamicActivity.this,SearchActivity.class);
        startActivity(intent);
    }
}
